/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DragBarTest.java
 *
 * Created on May 12, 2009, 11:25:40 AM
 */
package GeneralTesting;

import java.awt.Graphics;
import DragBarPanelPackage.*;
import ExpandablePopupMenuPackage.*;
import java.util.Vector;
import javax.swing.JTree;
import javax.swing.SwingUtilities;

/**
 *
 * @author Heather
 */
public class DragBarAndPopupTest extends javax.swing.JApplet
{

  /** Initializes the applet DragBarTest */
  public void init()
  {
    try
    {
      java.awt.EventQueue.invokeAndWait(new Runnable()
      {

        public void run()
        {
          setSize(600, 500);
          initComponents();
          dbPanel.setSize(testPanel.getSize());
          testPanel.add(dbPanel);
          // setup button popup listeners
          setupButtonPopupListeners();
        }
      });
    } catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }

  /** This method is called from within the init() method to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    testPanel = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();

    setBackground(new java.awt.Color(204, 204, 255));
    getContentPane().setLayout(null);

    javax.swing.GroupLayout testPanelLayout = new javax.swing.GroupLayout(testPanel);
    testPanel.setLayout(testPanelLayout);
    testPanelLayout.setHorizontalGroup(
      testPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 550, Short.MAX_VALUE)
    );
    testPanelLayout.setVerticalGroup(
      testPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 430, Short.MAX_VALUE)
    );

    getContentPane().add(testPanel);
    testPanel.setBounds(10, 10, 550, 430);

    jButton1.setText("clear");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });
    getContentPane().add(jButton1);
    jButton1.setBounds(10, 450, 200, 60);

    jButton2.setText("Add another button");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton2ActionPerformed(evt);
      }
    });
    getContentPane().add(jButton2);
    jButton2.setBounds(210, 450, 350, 60);
  }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
      // TODO add your handling code here:
      dbPanel.clearBG();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton2ActionPerformed
    {//GEN-HEADEREND:event_jButton2ActionPerformed
      // TODO add your handling code here:
      dbPanel.addNewButton("Another Button");
    }//GEN-LAST:event_jButton2ActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButton2;
  private javax.swing.JPanel testPanel;
  // End of variables declaration//GEN-END:variables
  private DragBarPanel dbPanel = new DragBarPanel(4);
  private boolean isBig = false;
  private int SMALL_SIZE = 358;
  private int BIG_SIZE = 590;
  // this is the expandable popup
  ExpandablePopupMenu exp = new ExpandablePopupMenu(new JTree());

  public void update(Graphics g)
  {
    System.out.println("I'm called");
    super.update(g);
    dbPanel.setSize(getSize());
  }

  public void setupButtonPopupListeners()
  {
    Vector<DragBarButton> theButtons = dbPanel.getButtons();

    for (int i = 0; i < theButtons.size(); i++)
    {
      final DragBarButton b = theButtons.get(i);
      b.addMouseListener(new java.awt.event.MouseAdapter()
      {

        public void mouseClicked(java.awt.event.MouseEvent evt)
        {
          if (SwingUtilities.isRightMouseButton(evt))
          {
            //pop.show(evt.getComponent(), evt.getX(), evt.getY());
            exp.show(evt.getComponent(), evt.getX(), evt.getY());
            dbPanel.repaint();
          }
        }
      });
    }
  }
}
